import os
from paramiko import SSHClient, AutoAddPolicy
from scp import SCPClient
import sys

# Nvx@2022  hz_dev/root  192.168.10.67
"""
scp -r /home/app/apollo_v2.3.0/apollo/apollo-app/apollo app@192.168.10.15:/home/app/apollo_v2.3.0/apollo/apollo-app/    
"""



scp_files = [
    'kernel/components/boosting/vertboost2/vert_xgboost_promoter.py',
    'kernel/components/boosting/vertboost2/vert_xgboost_provider.py',
    'kernel/components/boosting/vertboost2/xgb_predict.py'
]

log_dir = '/home/app/log/python_service'


dst_logs = r"D:\tmp\logs"


class Transport(object):
    def __init__(self, machine):
        self.machine = machine
        self.ssh = {}
        self.connect()

        self.promoter_li = []

    def connect(self):
        for i in self.machine:
            ssh = SSHClient()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            # ssh.load_system_host_keys()
            ssh.connect(hostname=i['hostname'], username=i['username'], password=i['password'])
            scp = SCPClient(ssh.get_transport(), progress=self.progress)
            self.ssh.update(
                {
                    i['hostname']: {
                        'ssh': ssh,
                        'workdir': i['workdir'],
                        'username': i['username'],
                        'scp': scp,
                        'member_id': i['member_id']
                    }
                }
            )

    @staticmethod
    def progress(filename, size, sent):
        sys.stdout.write("%s's progress: %.2f%%   \r" % (filename, float(sent) / float(size) * 100))

    def close(self):
        for h, v in self.ssh.items():
            v['scp'].close()
            v['ssh'].close()

    def push(self, src, dst):
        for h, v in self.ssh.items():
            print(f"{v['username']}@{h}:{dst}")
            v['scp'].put(src, '/'.join((v['workdir'], dst)))

    def pull(self, src, dst):
        for h, v in self.ssh.items():
            v['scp'].get(src, '/'.join((v['workdir'], dst)))

    def pull_logs(self, component_id, job_id):

        for h, v in self.ssh.items():
            m_type = 'promoter' if h in self.promoter_li else 'provider'
            src = '/'.join([log_dir, job_id, m_type, v['member_id'], component_id, 'DEBUG.log'])
            dst_dir = os.path.join(dst_logs, job_id)
            self.check_dir(dst_dir)
            dst = os.path.join(dst_dir, f"{h.split('.')[-1]}_DEBUG.log")
            print(src)
            print(dst)
            v['scp'].get(src, dst)

    @staticmethod
    def check_dir(dirname):
        """
        检测目录是否存在，不存在则创建
        :param dirname:
        :return:
        """
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        return dirname


if __name__ == '__main__':
    # project_dir = r"D:\project\ark"
    project_dir = r"/root/ark"
    machine_info = [
        {
            'hostname': '192.168.10.28',
            'username': 'app',
            'password': 'Nvx@123$',
            'workdir': '/home/app/apollo_v2.3.0/apollo/apollo-app/apollo',
            'member_id': '267c90830e26493c8177c1ac8e9ed091'
        },
        {
            'hostname': '192.168.10.15',
            'username': 'app',
            'password': 'Nvx@123$',
            'workdir': '/home/app/apollo_v2.3.0/apollo/apollo-app/apollo',
            'member_id': '6f62614c1a4c422981ee6c07e0f45247'
        },
        # {
        #     'hostname': '192.168.10.31',
        #     'username': 'app',
        #     'password': 'Nvx@123$',
        #     'workdir': '/home/app/apollo_v2.3.0/apollo/apollo-app/apollo',
        #     'member_id': '743fef8c611c48a69129e8a2cf4c2993'
        # },
        # {
        #     'hostname': '192.168.10.98',
        #     'username': 'app',
        #     'password': 'Nvx@123$',
        #     'workdir': '/home/app/apollo_v2.3.4/apollo/apollo-app/apollo',
        #     'member_id': 'bc87248609e14721a11fab88f7269ab7'
        # },
        # {
        #     'hostname': '192.168.10.99',
        #     'username': 'app',
        #     'password': 'Nvx@123$',
        #     'workdir': '/home/app/apollo_v2.3.4/apollo/apollo-app/apollo',
        #     'member_id': 'dce7c138a65c433f926eb42a4c12115f'
        # },
        # {
        #     'hostname': '192.168.10.10',
        #     'username': 'app',
        #     'password': 'Nvx@123$',
        #     'workdir': '/home/app/apollo_v2.3.7/apollo/apollo-app/apollo',
        #     'member_id': '23204327d1c245a792d09933b333d879'
        # },
        # {
        #     'hostname': '192.168.10.20',
        #     'username': 'app',
        #     'password': 'Nvx@123$',
        #     'workdir': '/home/app/apollo_v2.3.7/apollo/apollo-app/apollo',
        #     'member_id': 'c769a0a9013d4f7a80b4944d08e7cf2c'
        # },
    ]

    tp = Transport(machine_info)

    tp.promoter_li.append("192.168.10.28")
    # tp.pull_logs("VertBoost2_16764520812135120")

    # tp.pull_logs("HorzLR2_16770507640861106", 'd229bcbd454341eab097ed0055521c57')
    # tp.pull_logs("LinearRegression2_16793633540736706", 'a85464da12774d40b9ef05859f345bda')
    # exit(200)
    "common.python.calculation.spark.table.RDDSource"
    scp_files = [
        # # "kernel/task_executor2.py",
        # # "kernel/task_command.py",
        # "flow/web/preprocessor.py",
        # "flow/web/app.py",
        # "flow/web/utils/common.py",
        # "flow/app_launcher.py",
        # "flow/web/utils/pre_psi.py",
        # "common/python/db/db_models.py",
        # "common/python/db/data_psi_task_queue_dao.py",
        # # "common/python/db/data_psi_filter_queue_dao.py",
        # # "common/python/db/db_models.py",
        # # "flow/web/utils/pre_psi.py",
        # "kernel/tracker/tracking.py",


        # "common/python/utils/log_utils.py",
        'flow/cycle_actions/flow_action_queue/worker/start_job_worker.py',
        "flow/cycle_actions/flow_action_queue/worker/stop_job_worker.py",
        # "flow/cycle_actions/flow_action_queue/worker/resume_job_worker.py",
        "flow/service/job_scheduler/job_stop_action.py",
        'flow/service/job_scheduler/job_start_action.py',
        # "flow/service/job_scheduler/run_task_action.py",
        "common/python/db/db_models.py"

    ]

    for i in scp_files:
        tp.push(os.path.join(project_dir, i), i)
